import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:${USER}
 * Date:${YEAR}-${MONTH}-${DAY}
 * ${TIME}
 */
public class Main {
    int[] dx={0,0,-1,1};
    int[] dy={1,-1,0,0};
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        Queue<int[]> queue=new LinkedList<>();
        int prev=image[sr][sc];
        if(prev==color){
            return image;
        }
        queue.add(new int[]{sr,sc});
        int m=image.length;int n=image[0].length;
        while(!queue.isEmpty()){
            int[] tmp=queue.poll();
            int a=tmp[0];int b=tmp[1];
            image[a][b]=color;
            for(int i=0;i<4;i++){
                int x=a+dx[i];int y=b+dy[i];
                if(x>=0&&x<m&&y>=0&&y<n&&image[x][y]==prev){
                    queue.add(new int[]{x,y});
                }
            }
        }
        return image;
    }
}